//*************************************************************************************************
//************************ (C) 2001 - 2006, Eleven Engineering Incorporated ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  LEDs.asm
//**       Project:  XInc2 Library
//**       Created:  04 Jan 2003 by Ryan Northcott
//**       Revised:  08 Sep 2006 by Dan Beutel
//**
//**   Description:  Routines for using the LEDs on the XInC2 Development Board.  To use these
//**                 routines in your project, you must assign kDevLEDs_Semaphore to one of your
//**                 hardware semaphores.
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**	InitializeLEDs
//**	TurnOnLEDs
//**	TurnOffLEDs
//**	ToggleLEDs
//**	SetLEDs
//**
//*************************************************************************************************
//*************************************************************************************************

#ifndef __LIB_LEDS_ASM__
#define __LIB_LEDS_ASM__


//=============================================================================
// Input Params:	None
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	LED initialization
//=============================================================================
InitializeLEDs:
		st	r0, sp, 0
		st	r1, sp, 1
		st	r6, sp, 2
		add	sp, sp, 3
		
		mov	r0, kDevLED_PortA_Init
		outp	r0, kDevLED_PortA_Cfg
		
		mov	r0, kDevLED_PortC_Init
		outp	r0, kDevLED_PortC_Cfg
		
		mov	r0, kDevLED_PortD_Init
		outp	r0, kDevLED_PortD_Cfg
		
		mov	r1, 0
		jsr	r6, SetLEDs
		
		sub	sp, sp, 3
		ld	r0, sp, 0
		ld	r1, sp, 1
		ld	r6, sp, 2
		jsr	r6, r6


//=============================================================================
// Input Params:	r1 = LED Vector
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Turns on the LEDs that are specified by the bits set in r1.
//			The first LED is controlled by bit 0, the second by bit 1,
//			etc.  Only the lower 4 bits on Port A, the lower 2 bits on Port C, 
//			and bits 4 and 5 on port D map to LEDs.
//=============================================================================
TurnOnLEDs:
		st	r0, sp, 0
		st	r1, sp, 1
		st	r2, sp, 2

		xor	r1, r1, 0xFFFF	// Invert r1 (LEDs are active low)
		and	r2, r1, 0x00FF	// Mask the bits for the LED Ports

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUdown

		inp	r0, kDevLED_PortA
		and	r0, r0, r2	// Clear the bits for the LEDs on PortA we want to turn on
		outp	r0, kDevLED_PortA
		
		inp	r0, kDevLED_PortC
		and	r0, r0, r2	// Clear the bits for the LEDs on PortC we want to turn on
		outp	r0, kDevLED_PortC
		
		inp	r0, kDevLED_PortD
		and	r0, r0, r2	// Clear the bits for the LEDs on PortD we want to turn on
		outp	r0, kDevLED_PortD
		
		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUup

		ld	r0, sp, 0
		ld	r1, sp, 1
		ld	r2, sp, 2
		jsr	r6, r6


//=============================================================================
// Input Params:	r1 = LED Vector
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Turns off the LEDs that are specified by the bits set in r1.
//			The first LED is controlled by bit 0, the second by bit 1,
//			etc.  Only the lower 4 bits on Port A, the lower 2 bits on Port C, 
//			and bits 4 and 5 on port D map to LEDs.
//=============================================================================
TurnOffLEDs:
		st	r0, sp, 0
		st	r1, sp, 1
		st	r2, sp, 2

		and	r2, r1, 0x00FF	// Mask the bits for the LED Port

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUdown

		inp	r0, kDevLED_PortA_Cfg
		ior	r0, r0, r2		// Set the bits for the LEDs on Port A we want to turn off
		outp	r0, kDevLED_PortA_Cfg
		
		inp	r0, kDevLED_PortC_Cfg
		ior	r0, r0, r2		// Set the bits for the LEDs on Port C we want to turn off
		outp	r0, kDevLED_PortC_Cfg
		
		inp	r0, kDevLED_PortD_Cfg
		ior	r0, r0, r2		// Set the bits for the LEDs on Port D we want to turn off
		outp	r0, kDevLED_PortD_Cfg

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUup

		ld	r0, sp, 0
		ld	r1, sp, 1
		ld	r2, sp, 2
		jsr	r6, r6


//=============================================================================
// Input Params:	r1 = LED Vector
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Toggles the LEDs that are specified by the bits set in r1.
//			The first LED is controlled by bit 0, the second by bit 1,
//			etc.  Only the lower 4 bits on Port A, the lower 2 bits on Port C, 
//			and bits 4 and 5 on port D map to LEDs.
//=============================================================================
ToggleLEDs:
		st	r0, sp, 0
		st	r1, sp, 1
		st	r2, sp, 2

		and	r2, r1, 0x00FF	// Mask the bits for the LED Port

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUdown

		inp	r0, kDevLED_PortA
		xor	r0, r0, r2		// Toggle the bits for the LEDs on Port A we want to toggle
		outp	r0, kDevLED_PortA

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUup
		
		ld	r0, sp, 0
		ld	r1, sp, 1
		ld	r2, sp, 2
		jsr	r6, r6


//=============================================================================
// Input Params:	r1 = LED Vector
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Turns on the LEDs that are specified by bits set in r1 and
//			turns off the LEDs that are specified by bits cleared in r1. 
//			The first LED is controlled by bit 0, the second by bit 1,
//			etc.  Only the lower 4 bits on Port A, the lower 2 bits on Port C, 
//			and bits 4 and 5 on port D map to LEDs. 
//=============================================================================
SetLEDs:
		st	r0, sp, 0
		st	r1, sp, 1
		st	r2, sp, 2

		xor	r1, r1, 0xFFFF		// Invert r1 (LEDs are active low)
		and	r2, r1, 0x00FF		// Mask the bits for the LED Port

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUdown

		outp	r2, kDevLED_PortA		// Set the bits for the LEDs we want to turn on

		mov	r0, kDevLEDs_Semaphore
		outp	r0, SCUup

		ld	r0, sp, 0
		ld	r1, sp, 1
		ld	r2, sp, 2
		jsr	r6, r6

#endif